/**
* Copyright @ 2023 iAuto (Shanghai) Co., Ltd.
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are NOT permitted except as agreed by
* iAuto (Shanghai) Co., Ltd.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/

import { Reducer } from 'redux';
import { carModelIntegrate } from '@/utils/toolChainUtils';
import {
    getModelList,
} from '@/services/ability';

import {
    getPublishDetail,
    importExcelCheck,
    importExcel,
    getStatusRecordList
} from '@/services/soa';
import { getSocList } from '@/services/soc';
const RES_SUCCESS = '0';

export interface BaseModelState {
    data: any;
}
export interface BaseModelType {
    namespace: string;
    state: BaseModelState;
    effects: any;
    reducers: {
        save: Reducer<BaseModelState>;
    };
}
const abilityModel: BaseModelType = {
    namespace: "serviceModel",
    state: {
        data: {},
    },
    effects: {
        *getStatusRecordList({ payload }, { call }) {
            // console.log(payload);
            const res = yield call(getStatusRecordList, payload);
            return res;
        },
        *getImportExcelCheck({ payload }, { call }) {
            // console.log(payload);
            const res = yield call(importExcelCheck, payload);
            return res;
        },
        *postImportExcel({ payload }, { call }) {
            // console.log(payload);
            const res = yield call(importExcel, payload);
            return res;
        },
        *getSocListForVersion({ payload }, { call }) {
            // console.log(payload);
            const res = yield call(getSocList, payload);
            return res;
        },

        *getPublishDetail({ payload }, { call, put }) {
            const allCarModelListRes = yield call(getModelList);
            // 构建所有车系车型
            const { carModelTree, allCarModelMap } = carModelIntegrate(
                allCarModelListRes?.data?.map((item: any) => {
                    return {
                        ...item,
                        sCode: item.seriesCode,
                        mCode: item.modelCode,
                    }
                }
                )
            );
            const allCarModelList = carModelTree;
            // 获取已发布/未发布的车型soc信息
            const publishedCarRes = yield call(getPublishDetail, payload);
            const unpublishedCar = {};
            if (publishedCarRes && publishedCarRes?.statusCode == RES_SUCCESS) {
                if (Array.isArray(publishedCarRes?.data?.detail)) {
                    publishedCarRes.data.detail.forEach((detail: any) => {
                        const { sCode, unpublishCount } = detail;
                         // eslint-disable-next-line
                        if (unpublishedCar[sCode]) {
                        } else {
                            unpublishedCar[sCode] = {
                                unpublishCount,
                            };
                            unpublishedCar[sCode].models = {};
                            if (Array.isArray(detail.models)) {
                                detail.models.forEach((model: any) => {
                                    const { isPublish, socVersion } = model;
                                    unpublishedCar[sCode].models[model.mCode] = { isPublish, socVersion };
                                });
                            }
                        }
                    });
                }
                let defUnpublishSCode = '';
                let defUnpublishMCode = '';
                // 构造带红点的数据
                allCarModelList.forEach((series) => {
                    const { value: sCode } = series;
                    if (unpublishedCar[sCode]) {
                        series.labelNum = unpublishedCar[sCode].unpublishCount || 0;
                        if (defUnpublishSCode == '' && series.labelNum > 0) {
                            defUnpublishSCode = sCode;
                        }
                        allCarModelMap[sCode].unpublishCount = series.labelNum;
                        if (Array.isArray(series.children)) {
                            series.children.forEach((model: any) => {
                                const { value: mCode } = model;
                                if (
                                    unpublishedCar[sCode].models &&
                                    unpublishedCar[sCode].models[mCode]
                                ) {
                                    model.showDot = !unpublishedCar[sCode].models[mCode].isPublish;
                                    model.socVersion = unpublishedCar[sCode].models[mCode].socVersion;
                                    allCarModelMap[sCode].models[mCode].isPublish = !model.showDot;
                                    allCarModelMap[sCode].models[mCode].socVersion = model.socVersion;
                                    if (defUnpublishMCode == '' && !allCarModelMap[sCode].models[mCode].isPublish) {
                                        defUnpublishMCode = mCode;
                                    }
                                } else {
                                    model.showDot = !!0;
                                }
                            });
                        }
                    }
                });
                publishedCarRes.data.allCarModelMap = allCarModelMap;
                publishedCarRes.data.allCarModelList = allCarModelList;
                publishedCarRes.data.defUnpublish = { defUnpublishSCode, defUnpublishMCode };
            }
            yield put({
                type: 'saveCarModelList',
                payload: publishedCarRes.data,
            });
            return publishedCarRes;
        },
    },
    reducers: {
        save(state, action) {
            return {
                ...state,
                ...action.payload,
            };
        }
    },
};
export default abilityModel;
